{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6e3e8d9f-06a6-48f5-8136-b11a32040221",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# L2范数\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27700478-967f-447e-9745-76bd6ccebfc9",
   "metadata": {},
   "source": [
    "这段代码的核心目的是计算两个向量 $a$ 和 $b$ 的 $L_2$ 范数（欧几里得范数）。在数学上，$L_2$ 范数定义为向量中各元素平方和的平方根，即：\n",
    "\n",
    "$$\n",
    "\\| x \\|_2 = \\sqrt{\\sum_{i=1}^{n} x_i^2}\n",
    "$$\n",
    "\n",
    "其中，$x$ 是 $n$ 维向量，$x_i$ 是向量的各个分量。\n",
    "\n",
    "---\n",
    "\n",
    "### **步骤解析**\n",
    "\n",
    "1. **初始化向量**：\n",
    "   - 向量 $a$ 被定义为：\n",
    "     $$\n",
    "     a = \\begin{bmatrix} 3 \\\\ -4 \\end{bmatrix}\n",
    "     $$\n",
    "   - 向量 $b$ 被定义为：\n",
    "     $$\n",
    "     b = \\begin{bmatrix} 0 \\\\ 5 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "2. **计算 $L_2$ 范数**：\n",
    "   - 使用 `np.linalg.norm(a, ord=2)` 计算 $a$ 的 $L_2$ 范数：\n",
    "     $$\n",
    "     \\| a \\|_2 = \\sqrt{3^2 + (-4)^2} = \\sqrt{9 + 16} = \\sqrt{25} = 5\n",
    "     $$\n",
    "   - 另一种等效的计算方式是 `np.sqrt(a[0]**2 + a[1]**2)`，其数学表达式为：\n",
    "     $$\n",
    "     \\sqrt{3^2 + (-4)^2} = 5\n",
    "     $$\n",
    "   - 对于 $b$，使用 `np.linalg.norm(b, ord=2)` 计算：\n",
    "     $$\n",
    "     \\| b \\|_2 = \\sqrt{0^2 + 5^2} = \\sqrt{0 + 25} = \\sqrt{25} = 5\n",
    "     $$\n",
    "   - 同样， `np.sqrt(b[0]**2 + b[1]**2)` 计算：\n",
    "     $$\n",
    "     \\sqrt{0^2 + 5^2} = 5\n",
    "     $$\n",
    "\n",
    "---\n",
    "\n",
    "### **总结**\n",
    "这段代码计算的是 **欧几里得范数**，它表示向量从原点到终点的直线距离（即模长）。在二维情况下，它等价于使用 **勾股定理** 计算直角三角形的斜边长度。$L_2$ 范数在很多应用中具有重要意义，如最小二乘回归、信号处理和机器学习的损失计算。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "047869d8-ba1c-4683-8339-f9d21790b89e",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2dac3853-6f03-42cc-9ce8-9dc7b366b49b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66762b2b-cff2-4d5a-aac7-242bfb930147",
   "metadata": {},
   "source": [
    "## 定义向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f42cddaa-69ba-40e5-9828-6a18c6715101",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([3, -4])\n",
    "b = np.array([0, 5])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94ef4522-2867-406e-92a6-ea6cd4d14b46",
   "metadata": {},
   "source": [
    "## 计算 L2 范数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "255aa946-b983-4673-ac7e-fb1244debbf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "L2_a = np.linalg.norm(a, ord=2) # 默认"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c60db7d9-96f8-4dd6-bf58-09f64eba698c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(a[0]**2 + a[1]**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8ae91f0d-9d50-4af8-93de-bf9bd55ee2ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "L2_b = np.linalg.norm(b, ord=2) # 默认"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dbf08311-7b62-4ef4-81c2-93ab8d95c9c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(b[0]**2 + b[1]**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe6ec5e2-4368-4ffa-b86d-77c997af1e56",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
